package com.qf.common.utils;

import lombok.SneakyThrows;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 工具类操作String类型的公共方法类
 */
public class StringUtil {
    /**
     * 基本功能：过滤所有以"<"开头以">"结尾的标签
     * <p>
     *
     * @param str
     * @return String
     */
    private final static String regxpForHtml = "</([^>]*)>"; // 过滤所有以<开头以>结尾的标签
    private final static String regxpForHtml2 = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签

    /**
     * 判断字符串开头是否为中文
     */
    public static boolean isChinese(String str) {
        char[] strChar = str.toCharArray();
        char a = strChar[0];
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(a);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    public static String getUrlEndWith(String url) {
        String[] split = url.split("\\.");
        String end = split[split.length - 1].toLowerCase();
        return end;
    }

    //  此方法进行优化合并wos与pmid 统一转完到ut
    public static List<String> spWosAndPmidBakcList() {
        List<String> res = new ArrayList<>();
        return res;
    }

    /**
     * 判断字符有没有中文 暂且这样
     *
     * @param str
     * @return
     */
    public static Boolean isChina(String str) {
        if (null == str) {
            return false;
        }
        str = str.toLowerCase().trim();
        List<String> list = Arrays.asList("anhui ", "beijing ", "changchun ", "changsha ", "chengdu ", "china ", "chongqing ", "fujian ", "fuzhou ", "gansu ", "guangdong ", "guangxi ", "guangzhou ", "guiyang ", "guizhou ", "haikou ", "hainan ", "hangzhou ", "harbin ", "hebei ", "hefei ", "heilongjiang ", "henan ", "hohhot ", "hong kong ", "hubei ", "hunan ", "inner mongolia ", "jiangsu ", "jiangxi ", "jilin ", "jinan ", "kunming ", "lanzhou ", "lhasa ", "liaoning ", "macao ", "nanchang ", "nanjing ", "nanning ", "ningxia ", "qinghai ", "shaanxi ", "shandong ", "shanghai ", "shanxi ", "shenyang ", "shijiazhuang ", "sichuan ", "taipei ", "taiwan ", "taiyuan ", "tianjin ", "tibet ", "urumqi ", "wuhan ", "xi'an ", "xining ", "xinjiang ", "yinchuan ", "yunnan ", "zhejiang ", "zhengzhou ", "中国", "中华人民共和国", "北京", "浙江", "天津", "安徽", "上海", "福建", "重庆", "江西", "香港", "山东", "澳门", "河南", "内蒙古", "湖北", "新疆", "湖南", "宁夏", "广东", "西藏", "海南", "广西", "四川", "河北", "贵州", "山西", "云南", "辽宁", "陕西", "吉林", "甘肃", "黑龙江", "青海", "江苏", "台湾", "杭州", "合肥", "福州", "南昌", "济南", "郑州", "呼和浩特", "武汉", "乌鲁木齐", "长沙", "银川", "广州", "拉萨", "海口", "南宁", "成都", "石家庄", "贵阳", "太原", "昆明", "沈阳", "西安", "长春", "兰州", "哈尔滨", "西宁", "南京", "台北", "解放军", "重大专项", "温州", "深圳");
        for (String s : list) {
            if (str.contains(s)) {
                return true;
            }
        }
        return false;
    }

    @SneakyThrows
    public static String getDomain(HttpServletRequest request) {
        String remoteHost = request.getRequestURL().toString();
        URL url = new URL(remoteHost);
        String host = url.getHost();
        remoteHost = host;
        String S = remoteHost;
        String[] split = S.split("\\.");
        StringBuffer stringBuffer = new StringBuffer();
        if (split.length > 2) {
            for (int i = 0; i < split.length; i++) {
                if (i != 0) {
                    stringBuffer.append(split[i] + ".");
                }
            }
        }
        String domain = stringBuffer.toString().replace("/.", "");
        if (domain.endsWith(".")) {
            domain = domain.substring(0, domain.length() - 1);
        }
        return domain;
    }

    @SneakyThrows
    public static String getHost(HttpServletRequest request) {
        String remoteHost = request.getRequestURL().toString();
        URL url = new URL(remoteHost);
        String host = url.getHost();
        remoteHost = host;

        return remoteHost;
    }

    // 判断是不是测试项目
    public static boolean ispm6() {
        String classLoader = StringUtil.class.getResource("/").getPath();
        if (classLoader.contains("tomcat9-testpubmed-8118")) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断字符串是否为空
     */
    public static boolean isNotNull(String str) {
        if (str != null && !str.equals("") && !str.equals("null") && !str.equals("[]") && !str.trim().equals("") && !str.equals("[]") && !str.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断对象是否为空
     */
    public static boolean isNotNull(Object str) {
        if (str != null && !str.equals("") && !str.equals("null") && !str.equals("[]") && !str.toString().trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 数组 每页数量 当前页 全部页码 类型 0是整除 1是有余数
     *
     * @param ls
     * @param size
     * @param curPage
     * @param allPage
     * @param type
     * @return
     */
    public static List cutList(List ls, int size, int curPage, int allPage, int type) {
        List resLs;
        if (curPage != allPage) {
            resLs = ls.subList(((curPage - 1) * size), size * curPage);
        } else {
            if (type == 0) {
                resLs = ls.subList(((curPage - 1) * size), size * curPage);
            } else {
                resLs = ls.subList(((curPage - 1) * size), ls.size());
            }
        }
        return resLs;
    }

    /**
     * 判断字符串为空
     */
    public static boolean isNull(String str) {
        if (str == null || str.equals("") || str.trim().equals("null")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断字符串为空
     */
    public static boolean isNull(Object str) {
        if (str == null || str.toString().equals("") || str.toString().trim().equals("null")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * encodeUrl
     */
    public static String encodeUrl(String url) {
        try {
            url = URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * decodeUrl
     */
    public static String decodeUrl(String url) {
        try {
            url = URLDecoder.decode(url, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url;
    }





    public static String getDatePath(String pre) {
        Calendar calendar = Calendar.getInstance();
        String path = "";
        if (StringUtil.isNotNull(pre)) {
            if (pre.endsWith("/")) {
                path = pre + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + "/";
            } else {
                path = pre + "/" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + "/";
            }
        } else {
            path = "/sas/temp/" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DATE) + "/";

        }
        return path;
    }

    public static String filterHtml(String str) {
        Pattern pattern = Pattern.compile(regxpForHtml);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, "");
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String filterHtml2(String str) {
        Pattern pattern = Pattern.compile(regxpForHtml2);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, "/");
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
